package com.xing.fruit.servlet;

import com.xing.fruit.pojo.User;
import com.xing.fruit.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User loginUser = null;
        UserServiceImpl userService = new UserServiceImpl();
        //获取登录用户
        loginUser = userService.getLoginUser(username, password);
        //获取日期
        Date date = new Date();
        HttpSession session = req.getSession();
        session.setAttribute("date",date);
        System.out.println(date);
        if (loginUser!= null){
            session.setAttribute("user",loginUser);
            resp.sendRedirect("/jsp/user_center.jsp");
        }else {
            req.setAttribute("msg","用户名或密码错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }

    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doGet(req, resp);
    }
}
